<><template>
    <div class="warpper">
        <van-nav-bar safe-area-inset-top placeholder apper>
            <template #title>
                <van-col>
                    <van-icon name="points" size="20" color="red" />
                </van-col>
                <van-col>设置</van-col>
            </template>
        </van-nav-bar>

        <van-cell-group>
          <van-cell center title="检查数据库" is-link @click="checkVerison"/>
          <van-cell center title="重置数据库" is-link @click="isShowResetDialog = true"/>
          <van-cell center title="清除收藏" is-link @click="clearFavorited"/>
          <van-cell center title="清除权重" is-link @click="clearWeights"/>
          <van-cell center title="查看帮助" is-link @click="isShowHelpDialog=true"/>
          <van-cell center title="关于软件" is-link @click="isShowAboutDialog=true"/>
          <!-- <van-cell center title="检查更新" is-link @click="checkUpdate"/> -->
        </van-cell-group>

        <!-- 检查数据库 结果dialog -->
        <van-popup v-if="isShowCheckDataDialog" v-model="isShowCheckDataDialog" closeable round position="bottom" 
             transition-appear>
            <van-row class="CheckDataDialog-firstrow">
                <van-col :span="15" :offset="1">远端数据库版本:</van-col>
                <van-col :span="6">{{servVer}}</van-col>
            </van-row>

            <van-row>
                <van-col :span="15" :offset="1">本地数据库版本:</van-col>
                <van-col :span="6">{{localVer}}</van-col>
            </van-row>
            &nbsp;
            <van-row>
                <van-col :span="21" :offset="1">
                    如果数据库版本太低，可能是服务器缓存导致没有更新，请重置数据库(注意备份)。
                </van-col>
            </van-row>
        </van-popup>

        <!-- 重置数据库 提示dialog -->
        <van-popup v-if="isShowResetDialog" v-model="isShowResetDialog" closeable round position="bottom" 
             transition-appear>
            <van-row class="margin-top margin-bottom">
                <van-col :span="21" :offset="1" class="font-center"><h3>危险的操作!</h3></van-col>
            </van-row>

            <van-row class="margin-top margin-bottom">
                <van-col :span="21" :offset="1" class="font-center">重置数据库将删除本地所有保存的数据，并重置到初始状态</van-col>
            </van-row>

            <br><br>

            <van-row class="font-center margin-top">
                <van-button @click="isShowResetDialog = false" type="warning">取消</van-button>
                <van-button @click="isShowResetDialog = false;resetVersion()" >确定</van-button>
            </van-row>
            &nbsp;
        </van-popup>

        <!-- 重置数据库结束 dialog -->
        <van-popup v-if="isShowResetSuccessDialog" v-model="isShowResetSuccessDialog"  round position="bottom" 
            :close-on-click-overlay="false"  transition-appear>
            <van-row class="margin-top margin-bottom">
                <van-col :span="21" :offset="1" class="font-center"><h3>重置完成!</h3></van-col>
            </van-row>

            <van-row class="margin-top margin-bottom">
                <van-col :span="21" :offset="1" class="font-center">请重启程序!</van-col>
            </van-row>
        </van-popup>

        <!-- 帮助dialog -->
        <van-popup v-if="isShowHelpDialog" v-model="isShowHelpDialog" closeable round position="bottom"
             transition-appear>

            <div class="popup-title-container">
                <h3>前往帮助页面</h3>
            </div>

            <div class="popup-content-container" >
                <system-href ref="syshref" width='450' href="https://www.wolai.com/skyey-engrave/279NyNBi9oyAz73tUt7q9j"/> 

                <van-row class="font-center margin-top">
                    <van-button @click="isShowHelpDialog = false"  type="warning">取消</van-button>
                    <van-button @click='tohref()'>继续访问</van-button>
                </van-row>
            </div>
        </van-popup>

        <!-- 关于dialog -->
        <van-popup v-if="isShowAboutDialog" v-model="isShowAboutDialog" closeable round position="bottom"
             transition-appear>

            <div class="popup-title-container">
                <h3>刻印计算工具@{{version}} </h3>
                <h3> 开源软件
                    <span class="underline color-blue mouse-point"
                        @click="jump('https://gitee.com/zero_kk/skyeye_-omega/blob/master/LICENSE')">
                        @ISC License
                    </span>
                </h3>
            </div>

            <div class="popup-content-container" style="top:100px;">
                <van-row>authour:knciik
                    <span class="underline color-blue mouse-point" 
                        @click="jump('https://gitee.com/zero_kk')">
                        (https://gitee.com/zero_kk)
                    </span>
                </van-row>

                <van-row>
                    github:<span class="underline color-blue mouse-point" 
                        @click="jump('https://gitee.com/zero_kk/skyeye_-omega.git')">
                        https://gitee.com/zero_kk/skyeye_-omega.git
                    </span>
                </van-row>

                <van-row>
                    网页版:<span class="underline color-blue mouse-point"
                        @click="jump('http://zero_kk.gitee.io/skyey-engrave-pages/#/Search')">
                        http://zero_kk.gitee.io/skyey-engrave-pages/#/Search
                    </span>
                </van-row>
                <van-row>
                    发布地址:<span class="underline color-blue mouse-point"
                        @click="jump('https://wwd.lanzouy.com/b0371yeaf')">
                        https://wwd.lanzouy.com/b0371yeaf 提取吗:f8yj
                    </span>
                </van-row>

            </div>
        </van-popup>

        <!-- 更新appdialog -->
        <van-popup v-if="isShowDownloadUpdate" v-model="isShowDownloadUpdate" closeable round position="bottom"
             transition-appear>

            <div class="popup-title-container">
                <h3>检测到新版本,是否自动更新?</h3>
            </div>

            <div class="popup-content-container" >
                <system-href ref="syshref" width='450' href="https://www.wolai.com/skyey-engrave/279NyNBi9oyAz73tUt7q9j"/> 

                <van-row class="font-center margin-top">
                    <van-button @click="isShowDownloadUpdate = false"  type="warning">取消</van-button>
                    <van-button @click='tohref()'>继续</van-button>
                </van-row>
            </div>
        </van-popup>

    </div>
</template>

<script>
import { ContextServer,CalcServer } from '../server/servers';
import { Toast, Notify } from 'vant';
import Context from '../server/context'
const { ipcRenderer } = window.require('electron')
import SystemHref from '../sub/SystemHref.vue';

export default {
    name: "",
    components: {SystemHref},
    computed:{
        version:{
            get(){
                console.log("computed version",Context)
                return Context.getVersion()
            }
        }
    },  
    data:()=>({
        isShowCheckDataDialog: false,
        isShowResetDialog: false,
        isShowResetSuccessDialog: false,
        isShowHelpDialog: false,
        isShowAboutDialog: false,
        isShowDownloadUpdate:false,

        localVer:'',
        servVer:'',
    }),
    methods: {
        checkVerison() {
            Toast({
                message: '正在操作，请不要关闭程序',
                position: 'top',
                type: 'loading',
                overlay: true,
                duration: 0
            });

            ContextServer.getServerDataVersion().then(data=>{
                console.log("then",data)
                this.localVer = ContextServer.getLocalDataVersion() || 'error'
                this.servVer = data || 'error'
                setTimeout(()=>{
                    Toast.clear();
                    this.isShowCheckDataDialog = true
                },1000)
            })
        },

        resetVersion(){
            Toast({
                message: '正在操作，请不要关闭程序',
                position: 'top',
                type: 'loading',
                overlay: true,
                duration: 0
            });

            ContextServer.clearDatabase().then(()=>{
                setTimeout(()=>{
                    Toast.clear();
                    this.isShowResetSuccessDialog = true
                },1000)
            })
        },

        clearFavorited(){
            Toast({
                message: '正在操作，请不要关闭程序',
                position: 'top',
                type: 'loading',
                overlay: true,
                duration: 0
            });

            CalcServer.removeAllFavorited();
            setTimeout(()=>{
                Toast.clear();
                Notify({ type: 'success', message: '\n已清除收藏', duration: 2000 });
            },1000)
        },

        clearWeights(){
            Toast({
                message: '正在操作，请不要关闭程序',
                position: 'top',
                type: 'loading',
                overlay: true,
                duration: 0
            });

            CalcServer.removeCustomWeights();
            setTimeout(()=>{
                Toast.clear();
                Notify({ type: 'success', message: '\n已清除自定义权重', duration: 2000 });
            },1000)
        },

        checkUpdate(){
            Toast({
                message: '正在操作，请不要关闭程序',
                position: 'top',
                type: 'loading',
                overlay: true,
                duration: 0
            });

            ContextServer.checkAppPcUpdate().then(rs=>{
                if(rs){
                    Toast.clear();
                    this.isShowDownloadUpdate = true
                }else{
                    Toast.clear();
                    Notify({message:"\n当前为最新版本!",type:"success",duration:2000})
                }
            }).catch(e=>{
                console.log(e)
                Toast.clear();
                Notify({message:"\n更新失败,远端未响应",type:"warning",duration:2000})
            })
        },

        tohref(){
            ipcRenderer.send("open-url","https://www.wolai.com/skyey-engrave/279NyNBi9oyAz73tUt7q9j")
        },

        jump(url){
            ipcRenderer.send("open-url",url)
        },
    }
}
</script>

<style scoped>
.van-cell{
    text-align: center;
}

.van-cell .van-icon{
    display: none;
}
    
</style>

<style scoped>
.van-popup{
    height: 30%;
}

.van-button {
    width: 100px;
    height: 40px;
    line-height: 40px;
    padding: 0;
    text-align: center;
    font-size: 14px;
}
.ShowDialog-firstrow{
    margin-top: 40px;
}

.ShowDialog-firstrow .van-col{
    text-align: center;
}
.CheckDataDialog-firstrow{
    margin-top: 40px;
}

.popup-title-container {
    position: absolute;
    width: 500px;
    top: 0px;
    left: 0px;
}

.popup-title-container h3 {
    display: block;
    width: 500px;
    text-align: center;    
}

.popup-content-container{
    position: absolute;
    width: 450px;
    top: 60px;
    left: 0px;
    padding:0 5%;
}
</style>

